/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.updater.core
{
	import sweezy.anim.core.IPropertyAnimation;
	import sweezy.anim.core.IUpdater;
	import sweezy.anim.updater.UpdaterProperty;

	public class DefaultUpdater implements IUpdater
	{

		public function DefaultUpdater()
		{
		}

		public function initialize(element:Object, property:UpdaterProperty, prefix:String, option:Object, target:IPropertyAnimation):void
		{
			while (property !== null)
			{
				if (isNaN(property.end))
				{
					property.end = 0;
				}
				if (isNaN(property.start))
				{
					property.start = element[property.name];
				}
				if (property.relative)
				{
					property.end += property.start;
				}

				property = property.next;
			}
		}

		public function update(element:Object, property:UpdaterProperty, prefix:String, fraction:Number, option:Object, target:IPropertyAnimation):void
		{
			var value:Number;

			while (property !== null)
			{
				value = property.start + (property.end - property.start) * fraction;
				element[property.name] = property.round ? ((value * 2 | 0) - (value | 0)) : value;
				property = property.next;
			}
		}
	}
}
